package canglang.monitor.cases.action;

import canglang.monitor.core.base.action.BaseAction;
import canglang.monitor.core.base.pagination.PaginatedListHelper;
import canglang.monitor.core.base.util.DateUtil;
import canglang.monitor.core.base.util.FileUtil;
import canglang.monitor.core.base.util.StringUtil;
import canglang.monitor.cases.service.*;
import canglang.monitor.cases.model.*;
import canglang.monitor.user.model.UserModel;

import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.Date;
import java.io.OutputStream;

public class CaseSummaryAction extends BaseAction {

    private static final long serialVersionUID = -3692559716148974600L;

    private CaseLightMgr caseLightMgr;

    private CaseOneMgr caseOneMgr;

    private CaseTwoMgr caseTwoMgr;

    private CaseThreeMgr caseThreeMgr;

    private CaseFourMgr caseFourMgr;

    private CaseFiveMgr caseFiveMgr;

    private CaseSixMgr caseSixMgr;

    private CaseSevenMgr caseSevenMgr;

    private CaseEightMgr caseEightMgr;

    private CaseNineMgr caseNineMgr;

    private CaseTenMgr caseTenMgr;

    private CaseUploadMgr caseUploadMgr;

    private PaginatedListHelper helper;

    private List list;

    private List whiteList;

    private List bAttachList;

    private List pAttachList;

    private List yAttachList;

    private List rAttachList;

    private List blueList;

    private List purpleList;

    private List yellowList;

    private List redList;

    private String lightType;

    private String deptId;

    private String viewId;

    private String caseType;

    private String fromInd;

    private CaseSixModel caseSixModel;

    private CaseSevenModel caseSevenModel;

    private CaseEightModel caseEightModel;

    private CaseNineModel caseNineModel;

    private CaseTenModel caseTenModel;

    private CaseLightModel caseLightModel;

    private String downloadId;

    private String downloadType;

    private String downloadAttachId;

    private PublishUploadModel attach;

    private List uploadList;

    private String deleteFeedBackAttachId;

    public String getDeleteFeedBackAttachId() {
        return deleteFeedBackAttachId;
    }

    public void setDeleteFeedBackAttachId(String deleteFeedBackAttachId) {
        this.deleteFeedBackAttachId = deleteFeedBackAttachId;
    }

    public String getDownloadAttachId() {
        return downloadAttachId;
    }

    public void setDownloadAttachId(String downloadAttachId) {
        this.downloadAttachId = downloadAttachId;
    }

    public List getUploadList() {
        return uploadList;
    }

    public void setUploadList(List uploadList) {
        this.uploadList = uploadList;
    }

    public CaseUploadMgr getCaseUploadMgr() {
        return caseUploadMgr;
    }

    public void setCaseUploadMgr(CaseUploadMgr caseUploadMgr) {
        this.caseUploadMgr = caseUploadMgr;
    }

    public PublishUploadModel getAttach() {
        return attach;
    }

    public void setAttach(PublishUploadModel attach) {
        this.attach = attach;
    }

    public String getDownloadId() {
        return downloadId;
    }

    public void setDownloadId(String downloadId) {
        this.downloadId = downloadId;
    }

    public String getDownloadType() {
        return downloadType;
    }

    public void setDownloadType(String downloadType) {
        this.downloadType = downloadType;
    }

    public List getBAttachList() {
        return bAttachList;
    }

    public void setBAttachList(List bAttachList) {
        this.bAttachList = bAttachList;
    }

    public List getPAttachList() {
        return pAttachList;
    }

    public void setPAttachList(List pAttachList) {
        this.pAttachList = pAttachList;
    }

    public List getYAttachList() {
        return yAttachList;
    }

    public void setYAttachList(List yAttachList) {
        this.yAttachList = yAttachList;
    }

    public List getRAttachList() {
        return rAttachList;
    }

    public void setRAttachList(List rAttachList) {
        this.rAttachList = rAttachList;
    }

    public CaseLightModel getCaseLightModel() {
        return caseLightModel;
    }

    public void setCaseLightModel(CaseLightModel caseLightModel) {
        this.caseLightModel = caseLightModel;
    }

    public CaseNineModel getCaseNineModel() {
        return caseNineModel;
    }

    public void setCaseNineModel(CaseNineModel caseNineModel) {
        this.caseNineModel = caseNineModel;
    }

    public CaseTenModel getCaseTenModel() {
        return caseTenModel;
    }

    public void setCaseTenModel(CaseTenModel caseTenModel) {
        this.caseTenModel = caseTenModel;
    }

    public CaseSixModel getCaseSixModel() {
        return caseSixModel;
    }

    public void setCaseSixModel(CaseSixModel caseSixModel) {
        this.caseSixModel = caseSixModel;
    }

    public CaseSevenModel getCaseSevenModel() {
        return caseSevenModel;
    }

    public void setCaseSevenModel(CaseSevenModel caseSevenModel) {
        this.caseSevenModel = caseSevenModel;
    }

    public CaseEightModel getCaseEightModel() {
        return caseEightModel;
    }

    public void setCaseEightModel(CaseEightModel caseEightModel) {
        this.caseEightModel = caseEightModel;
    }

    public String getViewId() {
        return viewId;
    }

    public void setViewId(String viewId) {
        this.viewId = viewId;
    }

    public String getCaseType() {
        return caseType;
    }

    public void setCaseType(String caseType) {
        this.caseType = caseType;
    }

    public String getLightType() {
        return lightType;
    }

    public void setLightType(String lightType) {
        this.lightType = lightType;
    }

    public String getDeptId() {
        return deptId;
    }

    public void setDeptId(String deptId) {
        this.deptId = deptId;
    }

    public void setCaseLightMgr(CaseLightMgr caseLightMgr) {
        this.caseLightMgr = caseLightMgr;
    }

    public List getWhiteList() {
        return whiteList;
    }

    public void setWhiteList(List whiteList) {
        this.whiteList = whiteList;
    }

    public List getBlueList() {
        return blueList;
    }

    public void setBlueList(List blueList) {
        this.blueList = blueList;
    }

    public List getPurpleList() {
        return purpleList;
    }

    public void setPurpleList(List purpleList) {
        this.purpleList = purpleList;
    }

    public List getYellowList() {
        return yellowList;
    }

    public void setYellowList(List yellowList) {
        this.yellowList = yellowList;
    }

    public List getRedList() {
        return redList;
    }

    public void setRedList(List redList) {
        this.redList = redList;
    }

    public List getList() {
        return list;
    }

    public void setList(List list) {
        this.list = list;
    }

    public PaginatedListHelper getHelper() {
        return helper;
    }

    public void setHelper(PaginatedListHelper helper) {
        this.helper = helper;
    }

    public CaseSixMgr getCaseSixMgr() {
        return caseSixMgr;
    }

    public void setCaseSixMgr(CaseSixMgr caseSixMgr) {
        this.caseSixMgr = caseSixMgr;
    }

    public CaseSevenMgr getCaseSevenMgr() {
        return caseSevenMgr;
    }

    public void setCaseSevenMgr(CaseSevenMgr caseSevenMgr) {
        this.caseSevenMgr = caseSevenMgr;
    }

    public CaseEightMgr getCaseEightMgr() {
        return caseEightMgr;
    }

    public void setCaseEightMgr(CaseEightMgr caseEightMgr) {
        this.caseEightMgr = caseEightMgr;
    }

    public CaseNineMgr getCaseNineMgr() {
        return caseNineMgr;
    }

    public void setCaseNineMgr(CaseNineMgr caseNineMgr) {
        this.caseNineMgr = caseNineMgr;
    }

    public CaseTenMgr getCaseTenMgr() {
        return caseTenMgr;
    }

    public void setCaseTenMgr(CaseTenMgr caseTenMgr) {
        this.caseTenMgr = caseTenMgr;
    }

    public String getFromInd() {
        return fromInd;
    }

    public void setFromInd(String fromInd) {
        this.fromInd = fromInd;
    }

    public void setCaseOneMgr(CaseOneMgr caseOneMgr) {
        this.caseOneMgr = caseOneMgr;
    }

    public void setCaseTwoMgr(CaseTwoMgr caseTwoMgr) {
        this.caseTwoMgr = caseTwoMgr;
    }

    public void setCaseThreeMgr(CaseThreeMgr caseThreeMgr) {
        this.caseThreeMgr = caseThreeMgr;
    }

    public void setCaseFourMgr(CaseFourMgr caseFourMgr) {
        this.caseFourMgr = caseFourMgr;
    }

    public void setCaseFiveMgr(CaseFiveMgr caseFiveMgr) {
        this.caseFiveMgr = caseFiveMgr;
    }

    public String getClientSummary() {
        UserModel user = (UserModel) this.getSession().getAttribute("user");
        list = caseLightMgr.searchClientSummary();
        HashMap map = caseLightMgr.getAllActiveLights(user);
        whiteList = (ArrayList) map.get("white");
        blueList = (ArrayList) map.get("blue");
        purpleList = (ArrayList) map.get("purple");
        yellowList = (ArrayList) map.get("yellow");
        redList = (ArrayList) map.get("red");
        bAttachList = (ArrayList) map.get("bAttachList");
        pAttachList = (ArrayList) map.get("pAttachList");
        yAttachList = (ArrayList) map.get("yAttachList");
        rAttachList = (ArrayList) map.get("rAttachList");
        return SUCCESS;
    }

    public String getMoreSummary() {
        if (helper == null) {
            helper = new PaginatedListHelper();
            helper.setSortCriterion("createDt");
            helper.setSortDirection("desc");
        }
        helper.setObjectsPerPage(20);
        helper = caseLightMgr.searchMoreSummary(helper);
        return SUCCESS;
    }

    public String viewDetail() {
        if (caseType.equalsIgnoreCase("six")) {
            caseSixModel = caseSixMgr.getCaseSix(Integer.parseInt(viewId));
            return "six";
        } else if (caseType.equalsIgnoreCase("seven")) {
            caseSevenModel = caseSevenMgr.getCaseSeven(Integer.parseInt(viewId));
            return "seven";
        } else if (caseType.equalsIgnoreCase("eight")) {
            caseEightModel = caseEightMgr.getCaseEight(Integer.parseInt(viewId));
            return "eight";
        } else if (caseType.equalsIgnoreCase("nine")) {
            caseNineModel = caseNineMgr.getCaseNine(Integer.parseInt(viewId));
            return "nine";
        } else if (caseType.equalsIgnoreCase("ten")) {
            caseTenModel = caseTenMgr.getCaseTen(Integer.parseInt(viewId));
            return "ten";
        }
        return null;
    }

    public String removeLight() {
        caseLightMgr.removeLight(lightType, Integer.parseInt(deptId));
        getClientSummary();
        return SUCCESS;
    }

    public String addLight() {
        caseLightModel.setCaseId(0);
        caseLightModel.setLightInd("N");
        UserModel user = (UserModel) this.getSession().getAttribute("user");
        caseLightModel.setCreateId(String.valueOf(user.getDeptId()));
        caseLightModel.setUpdateId(String.valueOf(user.getDeptId()));
        Date date = DateUtil.getDate();
        caseLightModel.setCreateDt(date);
        caseLightModel.setUpdateDt(date);
        caseLightMgr.addCaseLight(caseLightModel);
        return SUCCESS;
    }

    public String download() throws Exception {
        String fileName = "";
        byte[] content = null;
        try {
            if (downloadType.equalsIgnoreCase("01")) {
                CaseOneModel c = caseOneMgr.getCaseOne(Integer.parseInt(downloadId));
                fileName = c.getUpload1FileName();
                content = c.getUpload1Cnt();
            } else if (downloadType.equalsIgnoreCase("02")) {
                CaseTwoModel c = caseTwoMgr.getCaseTwo(Integer.parseInt(downloadId));
                fileName = c.getUpload1FileName();
                content = c.getUpload1Cnt();
            } else if (downloadType.equalsIgnoreCase("03")) {
                CaseThreeModel c = caseThreeMgr.getCaseThree(Integer.parseInt(downloadId));
                fileName = c.getUpload1FileName();
                content = c.getUpload1Cnt();
            } else if (downloadType.equalsIgnoreCase("04")) {
                CaseFourModel c = caseFourMgr.getCaseFour(Integer.parseInt(downloadId));
                fileName = c.getUpload1FileName();
                content = c.getUpload1Cnt();
            } else if (downloadType.equalsIgnoreCase("05")) {
                CaseFiveModel c = caseFiveMgr.getCaseFive(Integer.parseInt(downloadId));
                fileName = c.getUpload1FileName();
                content = c.getUpload1Cnt();
            }
            getResponse().reset();
            getResponse().setHeader("Content-Disposition", "attachment; filename=\"" + new String(fileName.getBytes("gb2312"), "ISO8859-1"));
            getResponse().setContentType(DOWNLOAD_CONTENT_TYPE);
            getResponse().setContentLength(content.length);
            OutputStream out = getResponse().getOutputStream();
            out.write(content);
            out.flush();
        } catch (Exception e) {
            System.out.println(e);
            throw e;
        }
        return NONE;
    }

    public String viewUpload(){
        UserModel user = (UserModel) this.getSession().getAttribute("user");
        uploadList=caseUploadMgr.getUploadList(user.getDeptId());
        return SUCCESS;
    }

    public String uploadFeedbackAttach(){
        UserModel user = (UserModel) this.getSession().getAttribute("user");
        attach.setCreateId(String.valueOf(user.getUserId()));
        attach.setUpdateId(String.valueOf(user.getUserId()));
        attach.setDeptId(user.getDeptId());
        Date date = DateUtil.getDate();
        attach.setCreateDt(date);
        attach.setUpdateDt(date);
        attach.setUploadCnt(FileUtil.convertToByte(attach.getUpload()));
        caseUploadMgr.uploadFeedbackAttach(attach);
        return SUCCESS;
    }

    public String downloadFeedBackAttach() throws Exception {
       PublishUploadModel publishUploadModel = caseUploadMgr.getUploadAttach(Integer.parseInt(downloadAttachId));
        try {
            getResponse().reset();
            getResponse().setHeader("Content-Disposition", "attachment; filename=\"" + new String(publishUploadModel.getUploadFileName().getBytes("gb2312"), "ISO8859-1"));
            getResponse().setContentType(DOWNLOAD_CONTENT_TYPE);
            getResponse().setContentLength(publishUploadModel.getUploadCnt().length);
            OutputStream out = getResponse().getOutputStream();
            out.write(publishUploadModel.getUploadCnt());
            out.flush();
        } catch (Exception e) {
            System.out.println(e);
            throw e;
        }
        return NONE;
    }

    public String deleteFeedBackAttach() throws Exception{
        String[] arr=deleteFeedBackAttachId.split("\\|");
        for(int i=0;i<arr.length;i++){
            if(!StringUtil.isNullOrBlank(arr[i])){
                caseUploadMgr.deleteFeedbackAttach(Integer.parseInt(arr[i]));   
            }
        }
        return SUCCESS;
    }

}